#include "global.h"
#include <QMessageBox>
#include <QString>
#include <QDebug>
#include <QByteArray>


unsigned char host_addr = 100;
unsigned char local_addr = 1;
GraButtonNumAllocation num_allocate;

void print(QByteArray tmp)
{
    qDebug("length = %d",tmp.size());
    for(int j = 0; j < tmp.size(); ++j)
    {
        quint8 val = tmp[j];
        qDebug("%d : %d",j,val);
    }
}

//====================================================================================
//  implementation of class GraButtonNumAllocation
//====================================================================================
void GraButtonNumAllocation::mark_id(int id, bool flag)
{
    if( id<0 || id>MAX_NUM_GRA_BTN )
    {
        qDebug()<<"invalid ID";
        return;
    }
    if( flag )
    {
        numbers.setBit(id,true);
    }
    else
    {
        numbers.setBit(id,false);
    }
}

void GraButtonNumAllocation::updata_tail(int new_tail , int increament = 1 )
{
    if( new_tail>=0 && new_tail<=MAX_NUM_GRA_BTN )
    {
        tail = new_tail;
        item_count += increament;
    }
}

int GraButtonNumAllocation::allocate_num()
{
    if(item_count == MAX_NUM_GRA_BTN)
    {
        qDebug()<< "当前系统手控点数目已达上限1800无法继续添加!";
        return 0;
    }

    int cnt = (tail+1)%(MAX_NUM_GRA_BTN+1);
    for(; ;)
    {
        if(numbers.testBit(cnt))
        {
            cnt = (cnt+1)%(MAX_NUM_GRA_BTN+1);
        }
        else
        {
            return cnt;
        }
    }
}

void GraButtonNumAllocation::clear_all()
{
    numbers.fill(false);
    numbers.setBit(0,true);
    tail = 0;
    item_count = 0;
}

void GraButtonNumAllocation::used_num()
{
    for(int i = 0; i<numbers.size(); i++)
    {
        if(numbers.testBit(i))
            qDebug()<<i;
    }
}



//====================================================================================
//  implementation of class CurrentBtnRcd
//====================================================================================

int CurrentBtnRcd::getMinID()
{
    if(records.size()!=0)
        return records.first();
    else
        return INVALID_MIN_ID;
}

int CurrentBtnRcd::getMaxID()
{
    if( records.size() !=0 )
        return records.last();
    else
        return INVALID_MAX_ID;
}

void CurrentBtnRcd::eraseAllRecord()
{
    records.erase(records.begin(), records.end());
}

bool CurrentBtnRcd::addOneRecord( int btnID, VectorPos pos ,int sys_mode)
{
    if(sys_mode==SysMode::FULL_MODE)
    {
        if( records.size() == NUM_OF_BTN_PER_PAGE )
        {
            qDebug()<<"the current page could only hold "<<NUM_OF_BTN_PER_PAGE<<" graphic btns";
            return false;
        }
    }
    else if(sys_mode==SysMode::SIMPLE_MODE)
    {
        if( records.size() == NUM_OF_BTN )
        {
            qDebug()<<"the current page could only hold "<<NUM_OF_BTN<<" graphic btns";
            return false;
        }
    }
    else
        return false;

    if(pos == HEAD)
        records.push_front(btnID);
    else if(pos == TAIL)
        records.push_back(btnID);
    else
        return false;

    return true;
}

bool CurrentBtnRcd::isRecordExist(int btnID)
{
    if( btnID>getMaxID() || btnID<getMinID() )
        return false;
    else
        return true;
//    for(int index = 0; index < records.size(); ++index)
//    {
//        if(records[index]==btnID)
//            return true;
//    }
//    return false;
}



//====================================================================================
//  implementation of class SysMode
//====================================================================================

SysMode* SysMode::Instance()
{
    static SysMode _instance;//静态局部变量，在程序结束时候释放
    return &_instance;
}

void SysMode::setMode(int _mode)
{
    if( _mode==FULL_MODE || _mode==SIMPLE_MODE )
        sys_mode = _mode;
}

int SysMode::getMode()
{
    return sys_mode;
}


//====================================================================================
//  implementation of class NodeStatus
//====================================================================================

bool NodeStatus::isNodeDefined(int node_num)
{
    //节点状态表中有记录，则表示该手控点已经定义
    return node_status_table.find(node_num) != node_status_table.end();
}

bool NodeStatus::isNodeForbid(int node_num)
{
    unsigned int n_status = node_status_table.find(node_num).value();
    if ( n_status&FORBID_BIT )
        return true;
    return false;
}

bool NodeStatus::isNodeFault(int node_num)
{
    unsigned int n_status = node_status_table.find(node_num).value();
    if ( n_status&FAULT_BIT )
        return true;
    return false;
}

bool NodeStatus::isNodeStart(int node_num)
{
    unsigned int n_status = node_status_table.find(node_num).value();
    if ( n_status&START_BIT )
        return true;
    return false;
}

bool NodeStatus::isNodeFedback(int node_num)
{
    unsigned int n_status = node_status_table.find(node_num).value();
    if ( n_status&FEDBACK_BIT )
        return true;
    return false;
}

void NodeStatus::setNodeStatus(int node_num, unsigned int option)
{
    if( isNodeDefined(node_num) )
    {
        node_status_table[node_num]  |= option;
        qDebug()<<"node_status_table[node_num] = "<<node_status_table[node_num];
    }
}

void NodeStatus::resetNodeStatus(int node_num, unsigned int option)
{
    if( isNodeDefined(node_num) )
    {
        node_status_table[node_num]  &= option;
    }
}

bool NodeStatus::add(int node_number, unsigned int status)
{
    if( node_status_table.find(node_number)!=node_status_table.end() )
        return false;
    node_status_table.insert(node_number,status);
    return true;
}

bool NodeStatus::remove(int node_number)
{
    if( node_status_table.find(node_number) == node_status_table.end() )
        return false;
    return node_status_table.remove(node_number);
}
